home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pine / osdep / pipe.win < prev    next >
Text File  |  1996-05-02  |  4KB  |  164 lines

  1. #line 2 "osdep/pipe.win"
  2.  
  3.  
  4.  
  5. /*======================================================================
  6.     pipe
  7.     
  8.     Initiate I/O to and from a process.  These functions are similar to 
  9.     popen and pclose, but both an incoming stream and an output file are 
  10.     provided.
  11.    
  12.  ====*/
  13.  
  14.  
  15.  
  16. /*----------------------------------------------------------------------
  17.      Pipe a single character
  18.  
  19.   Args: command -- string to hand the shell
  20.     outfile -- address of pointer containing file to receive output
  21.     errfile -- file to write stderr to
  22.     mode -- mode for type of shell, signal protection etc...
  23.   Returns: NULL on DOS
  24.  
  25.  ----*/
  26. PIPE_S *
  27. open_system_pipe(command, outfile, errfile, mode)
  28.      char  *command;
  29.      char **outfile;
  30.      char **errfile;
  31.      int    mode;
  32. {
  33.     char cmdbuf[1024], *p;
  34.     PIPE_S *syspipe = NULL;
  35.  
  36.     if((mode & (PIPE_READ | PIPE_WRITE)) == (PIPE_READ | PIPE_WRITE)
  37.        || errfile){
  38.     q_status_message1(SM_ORDER, 3, 3, "Unsupported Pipe arg: %s",
  39.               errfile ? "Error File" : "Read and Write");
  40.     return(NULL);                /* UNSUPPORTED */
  41.     }
  42.  
  43.     if(mode & (PIPE_RESET | PIPE_READ | PIPE_WRITE)){
  44. #ifdef    WIN32
  45.     q_status_message(SM_ORDER, 3, 3,
  46.              "Pipe option not supported under NT yet");
  47.     return(NULL);                /* UNSUPPORTED */
  48. #else
  49.     sprintf(cmdbuf, "dosprmpt.pif /c %s", command);
  50. #endif
  51.     }
  52.     else
  53.       strcpy(cmdbuf, command);
  54.  
  55.     syspipe = (PIPE_S *)fs_get(sizeof(PIPE_S));
  56.     memset(syspipe, 0, sizeof(PIPE_S));
  57.     syspipe->mode = mode;
  58.  
  59.     if(mode & PIPE_WRITE){
  60.     /*
  61.      * Create tmp file to write, spawn child in close_pipe
  62.      * after tmp file's written...
  63.      */
  64.     syspipe->tmp   = temp_nam(NULL, "pw");
  65.     syspipe->out.f = fopen(syspipe->tmp, "wb");
  66.     strcat(cmdbuf, " < ");
  67.     strcat(cmdbuf, syspipe->tmp);
  68.     if(outfile){
  69.         if(!*outfile)
  70.           *outfile = temp_nam(NULL, "po");    /* asked for, but not named? */
  71.  
  72.         strcat(cmdbuf, " > ");
  73.         strcat(cmdbuf, *outfile);
  74.     }
  75.  
  76.     syspipe->command = cpystr(cmdbuf);
  77.     dprint(1, (debugfile, "pipe write: %s", cmdbuf));
  78.     }
  79.     else if(mode & PIPE_READ){
  80.     /* 
  81.      * Create a tmp file for command result, exec the command
  82.      * here into temp file, and return file pointer to it...
  83.      */
  84.     strcat(cmdbuf, " > ");
  85.     if(outfile){
  86.         if(!*outfile)
  87.           *outfile = temp_nam(NULL, "po");    /* asked for, but not named? */
  88.  
  89.         strcat(cmdbuf, *outfile);
  90.     }
  91.     else{
  92.         syspipe->tmp = temp_nam(NULL, "pr");
  93.         strcat(cmdbuf, syspipe->tmp);
  94.     }
  95.  
  96.     dprint(1, (debugfile, "pipe read: %s", cmdbuf));
  97.     if(mswin_exec_and_wait("pipe command", cmdbuf)){
  98.         if(syspipe->tmp)
  99.           fs_give((void **)&syspipe->tmp);
  100.  
  101.         fs_give ((void **)&syspipe);
  102.     }
  103.     else
  104.       syspipe->in.f = fopen(syspipe->tmp, "rb");
  105.     }
  106.     else{
  107.     /* we just run the command taking outfile into account */
  108.     if(outfile){
  109.         if(!*outfile)
  110.           *outfile = temp_nam(NULL, "po");    /* asked for, but not named? */
  111.  
  112.         strcat(cmdbuf, " > ");
  113.         strcat(cmdbuf, *outfile);
  114.     }
  115.  
  116.     if(mswin_exec_and_wait("pipe command", cmdbuf)){
  117.         fs_give ((void **)&syspipe);
  118.     }
  119.     }
  120.  
  121.     return(syspipe);
  122. }
  123.  
  124.  
  125.  
  126. /*----------------------------------------------------------------------
  127.     Close pipe previously allocated and wait for child's death
  128.  
  129.   Args: syspipe -- address of pointer to struct returned by open_system_pipe
  130.   Returns: 0 on DOS
  131.  ----*/
  132. int
  133. close_system_pipe(syspipe)
  134.     PIPE_S **syspipe;
  135. {
  136.     if(!(syspipe && *syspipe))
  137.       return(-1);
  138.  
  139.     if((*syspipe)->mode & PIPE_WRITE){
  140.     int rv;
  141.  
  142.     fclose((*syspipe)->out.f);
  143.     if(rv = mswin_exec_and_wait("pipe command", (*syspipe)->command))
  144.       q_status_message1(SM_ORDER, 3, 3,
  145.                 "Error executing external command: %s",
  146.                 (rv < 0)
  147.                   ? "Windows specific error"
  148.                   : error_description(errno));
  149.  
  150.     fs_give((void **)&(*syspipe)->command);
  151.     }
  152.     else if((*syspipe)->mode & PIPE_READ){
  153.     fclose((*syspipe)->in.f);
  154.     }
  155.  
  156.     if((*syspipe)->tmp){
  157.     unlink((*syspipe)->tmp);
  158.     fs_give((void **)&(*syspipe)->tmp);
  159.     }
  160.  
  161.     fs_give((void **)syspipe);
  162.     return(0);
  163. }
  164.